package com.zr.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.zr.dao.DgNewDao;
import com.zr.model.Content;
import com.zr.util.GetJDBCUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
/**
 * 获取最新入库知识的数据库操作层实现
 * @author Administrator
 *
 */
public class DgNewDaoImpl implements DgNewDao{

	/**
	 * 获取最新入库知识的数据库操作层实例方法
	 */
	GetJDBCUtil gt = new GetJDBCUtil();
	
	@Override
	public JSONObject getDgNew() {
		Connection con = gt.getJDBCConnection();
		
		JSONObject jObject = new JSONObject();
		jObject.put("total", 10);
		JSONArray jArray = new JSONArray();
		
		StringBuffer sql = new StringBuffer(""); 
		sql.append("SELECT * ");
		sql.append("FROM e_content ");
		sql.append("ORDER BY e_content.ctime DESC ");
		sql.append("LIMIT 0,10 ");
		
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			
			ResultSet rs = pst.executeQuery();
			
			while(rs.next()){
				Content content = new Content();
				content.setCid(rs.getInt("cid"));
				content.setCtitle(rs.getString("ctitle"));
				content.setCcontent(rs.getString("ccontent"));
				jArray.add(content);
			}
			
			jObject.put("rows", jArray);
			
			rs.close();
			pst.close();
			con.close();
		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return jObject;
	}

}
